﻿using System;
using System.Windows.Media.Imaging;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using Autodesk.Windows;
using YArchitech.Controls;
using VerticalMenu.Commands;
using VerticalMenu.Utility;

namespace VerticalMenu
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class VerticalMenuApplication : IExternalApplication
	{
		public Result OnStartup(UIControlledApplication application)
		{
			int num = Convert.ToInt32(application.ControlledApplication.VersionNumber);
			bool flag = num < 2015;
			Result result;
			if (flag)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			else
			{
				VerticalMenuApplication.m_UICtrlApplication = application;
				VerticalMenuApplication.m_ThisApp = this;
				bool flag2 = !CommandInfoReader.GetCommandInfos(ref VerticalMenuApplication.m_CommandTableInfos);
				if (flag2)
				{
					YJKMessageBox.Show("未提取到菜单信息，初始化竖向菜单失败");
					result = Autodesk.Revit.UI.Result.Failed;
				}
				else
				{
					this.RegisterPage(application, VerticalMenuApplication.m_CommandTableInfos);
					application.ControlledApplication.DocumentOpened += new EventHandler<DocumentOpenedEventArgs>(this.app_IniMethod);
					application.ControlledApplication.DocumentCreated += new EventHandler<DocumentCreatedEventArgs>(this.app_IniMethod);
					application.Idling += new EventHandler<IdlingEventArgs>(this.app_Idling);
					application.ApplicationClosing += new EventHandler<ApplicationClosingEventArgs>(this.app_ClosingMethod);
					application.DockableFrameVisibilityChanged += this.DockableFrameVisibilityChanged;
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			return result;
		}

		private void DockableFrameVisibilityChanged(object sender, DockableFrameVisibilityChangedEventArgs e)
		{
			bool flag = Globals.sm_UserDockablePaneId == e.PaneId;
			if (flag)
			{
				bool flag2 = !e.DockableFrameShown;
				if (flag2)
				{
					StateController.SaveState(StateController.StateEnum.SE_Hide);
				}
			}
		}

		public Result OnShutdown(UIControlledApplication application)
		{
			int num = Convert.ToInt32(application.ControlledApplication.VersionNumber);
			bool flag = num < 2015;
			Result result;
			if (flag)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			else
			{
				bool flag2 = VerticalMenuApplication.m_MainPage != null;
				if (flag2)
				{
					VerticalMenuApplication.m_MainPage.SetCommandData(null);
				}
				application.ControlledApplication.DocumentOpened -= new EventHandler<DocumentOpenedEventArgs>(this.app_IniMethod);
				application.ControlledApplication.DocumentCreated -= new EventHandler<DocumentCreatedEventArgs>(this.app_IniMethod);
				application.Idling -= new EventHandler<IdlingEventArgs>(this.app_Idling);
				application.ApplicationClosing -= new EventHandler<ApplicationClosingEventArgs>(this.app_ClosingMethod);
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			return result;
		}

		private void AddVerticalMenuToRibbonTable(UIControlledApplication application, int hyRibbonTableCount)
		{
			try
			{
				application.CreateRibbonTab("竖向菜单");
				RibbonPanel ribbonPanel = application.CreateRibbonPanel("竖向菜单", "竖向菜单面板");
				ribbonPanel.AddSeparator();
				string str = FileUtility.GetAppRootPath() + "\\Res\\LargeImage\\";
				PushButton pushButton = ribbonPanel.AddItem(new PushButtonData("显示菜单", "显示菜单", FileUtility.GetAssemblyFullName(), typeof(ExternalCommandShowPage).FullName)
				{
					LargeImage = new BitmapImage(new Uri(str + "显示竖向菜单.png"))
				}) as PushButton;
				pushButton.AvailabilityClassName = typeof(ExternalCommandShowPage).FullName;
				PushButton pushButton2 = ribbonPanel.AddItem(new PushButtonData("隐藏菜单", "隐藏菜单", FileUtility.GetAssemblyFullName(), typeof(ExternalCommandHidePage).FullName)
				{
					LargeImage = new BitmapImage(new Uri(str + "隐藏竖向菜单.png"))
				}) as PushButton;
				pushButton2.AvailabilityClassName = typeof(ExternalCommandHidePage).FullName;
				foreach (RibbonTab ribbonTab in ComponentManager.Ribbon.Tabs)
				{
					bool flag = ribbonTab.Title.CompareTo("竖向菜单") == 0;
					if (flag)
					{
						ComponentManager.Ribbon.Tabs.Remove(ribbonTab);
						ComponentManager.Ribbon.Tabs.Insert(hyRibbonTableCount, ribbonTab);
						break;
					}
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Show("竖向菜单RibbonPanel创建失败");
			}
		}

		private void RegisterDockableWindow(UIControlledApplication application, Guid mainPageGuid)
		{
			Globals.sm_UserDockablePaneId = new DockablePaneId(mainPageGuid);
			application.RegisterDockablePane(Globals.sm_UserDockablePaneId, "竖向菜单", VerticalMenuApplication.m_MainPage);
			bool flag = DockablePane.PaneExists(Globals.sm_UserDockablePaneId);
			if (flag)
			{
				DockablePane dockablePane = application.GetDockablePane(Globals.sm_UserDockablePaneId);
				bool flag2 = dockablePane != null;
				if (flag2)
				{
					dockablePane.Hide();
				}
			}
		}

		public bool CreateWindow(CommandTableInfos commandTableInfos)
		{
			bool result;
			try
			{
				VerticalMenuApplication.m_MainPage = new MainPage(commandTableInfos);
				result = true;
			}
			catch (Exception)
			{
				VerticalMenuApplication.m_MainPage = null;
				YJKMessageBox.Show("竖向菜单窗口创建失败");
				result = false;
			}
			return result;
		}

		public void app_Idling(object obj, EventArgs args)
		{
			bool flag = StateController.GetState() == StateController.StateEnum.SE_Show;
			if (flag)
			{
				MainPage.Refresh();
			}
		}

		public void app_IniMethod(object obj, EventArgs args)
		{
			DocumentCreatedEventArgs documentCreatedEventArgs = args as DocumentCreatedEventArgs;
			bool flag = documentCreatedEventArgs != null && documentCreatedEventArgs.Document.IsFamilyDocument;
			if (!flag)
			{
				DocumentOpenedEventArgs documentOpenedEventArgs = args as DocumentOpenedEventArgs;
				bool flag2 = documentOpenedEventArgs != null && documentOpenedEventArgs.Document.ActiveView == null;
				if (!flag2)
				{
					StateController.StateEnum state = StateController.GetState();
					bool flag3 = state == StateController.StateEnum.SE_Uninit_Show;
					if (flag3)
					{
						PressKeys.Press("XSSXCD", false);
					}
				}
			}
		}

		public void app_ClosingMethod(object obj, EventArgs args)
		{
			StateController.StateEnum state = StateController.StateEnum.SE_Uninit_Hide;
			bool flag = StateController.GetState() == StateController.StateEnum.SE_Show;
			if (flag)
			{
				PressKeys.Press("HID", false);
				state = StateController.StateEnum.SE_Uninit_Show;
			}
			StateController.SaveState(state);
		}

		public void SetWindowVisibility(ExternalCommandData commandData, bool state)
		{
			DockablePane dockablePane = commandData.Application.GetDockablePane(Globals.sm_UserDockablePaneId);
			bool flag = dockablePane == null;
			if (flag)
			{
				throw new Autodesk.Revit.Exceptions.InvalidOperationException("无法识别可停靠菜单面板");
			}
			bool flag2 = !this.IsMainWindowAvailable();
			if (flag2)
			{
				throw new Autodesk.Revit.Exceptions.InvalidOperationException("竖向菜单未创建");
			}
			if (state)
			{
				dockablePane.Show();
				bool flag3 = VerticalMenuApplication.m_MainPage != null;
				if (flag3)
				{
					VerticalMenuApplication.m_MainPage.SetCommandData(commandData);
				}
			}
			else
			{
				dockablePane.Hide();
				bool flag4 = VerticalMenuApplication.m_MainPage != null;
				if (flag4)
				{
					VerticalMenuApplication.m_MainPage.SetCommandData(null);
				}
			}
		}

		public bool IsMainWindowAvailable()
		{
			bool flag = VerticalMenuApplication.m_MainPage == null;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				bool flag2 = true;
				try
				{
					bool isVisible = VerticalMenuApplication.m_MainPage.IsVisible;
				}
				catch (Exception)
				{
					flag2 = false;
				}
				result = flag2;
			}
			return result;
		}

		public void RegisterPage(UIControlledApplication application, CommandTableInfos commandTableInfos)
		{
			bool flag = !this.CreateWindow(commandTableInfos);
			if (!flag)
			{
				VerticalMenuApplication.m_MainPage.SetInitialDockingParameters(0, 150, 0, 700, 59420, Guid.Empty);
				try
				{
					this.RegisterDockableWindow(application, Globals.sm_UserDockablePaneId.Guid);
				}
				catch (Exception ex)
				{
					TaskDialog.Show("竖向菜单", ex.Message);
				}
			}
		}

		internal static MainPage m_MainPage = null;

		internal static UIControlledApplication m_UICtrlApplication = null;

		internal static VerticalMenuApplication m_ThisApp = null;

		internal static CommandTableInfos m_CommandTableInfos = null;
	}
}
